
public class demo1 {
    public class ListNode{
        private ListNode next;
        private int val;

        public ListNode(int val) {
            this.val = val;
        }

        public ListNode(ListNode next, int val) {
            this.next = next;
            this.val = val;
        }
    }
    public ListNode reverseList(ListNode head) {
        ListNode newHead=new ListNode(-1);
        ListNode cur=head;
        while (cur!=null){
            ListNode newcur=cur.next;
            cur.next=newHead.next;
            newHead.next=cur;
            cur=newcur;
        }
        return newHead.next;
    }
}
